#define THIS_FILE "fn_x_update_dlg.sqf"
#include "x_setup.sqf"
private ["_display","_mr1text","_mr2text","_mr1_available","_mr2_available","_target_array2","_tl_available","_camps","_camps_available"];

if (GVAR(x_loop_end)) exitWith {};

disableSerialization;

#define __CTRL(A) (_display displayCtrl A)

_display = __uiGetVar(X_TELE_DIALOG);

_mr1text = __CTRL(100105);
_mr2text = __CTRL(100106);

_mr1_available = true;
_mr2_available = true;
// TYRGHEN
_tl_available = false;
_camps_available = false;

_mr1text ctrlSetText "";
_mr2text ctrlSetText "";

_mrs1 = missionNamespace getVariable (GVAR(mob_respawns) select 0);

_is_in_air = _mrs1 getVariable [QGVAR(in_air), false];

switch (true) do {
	case (_is_in_air): {
		_mr1text ctrlSetText (localize "STR_DOM_MISSIONSTRING_592");
		_mr1_available = false;
	};
	case (speed _mrs1 > 4): {
		_mr1text ctrlSetText (localize "STR_DOM_MISSIONSTRING_593");
		_mr1_available = false;
	};
	case (surfaceIsWater [(getPosASL _mrs1) select 0,(getPosASL _mrs1) select 1]): {
		_mr1text ctrlSetText (localize "STR_DOM_MISSIONSTRING_594");
		_mr1_available = false;
	};
	case (!alive _mrs1): {
		_mr1text ctrlSetText (localize "STR_DOM_MISSIONSTRING_595");
		_mr1_available = false;
	};
	default {
		_depl = GV(_mrs1,GVAR(MHQ_Deployed));
		if (isNil "_depl" || {!_depl}) then {
			_mr1text ctrlSetText (localize "STR_DOM_MISSIONSTRING_596");
			_mr1_available = false;
		} else {
			_enear = GV(_mrs1,GVAR(enemy_near));
			if (!isNil "_enear" && {_enear}) then {
				_mr1text ctrlSetText (localize "STR_DOM_MISSIONSTRING_597");
				_mr1_available = false;
			};
		};
	};
};

_mrs2 = missionNamespace getVariable (GVAR(mob_respawns) select 1);
_is_in_air = _mrs2 getVariable [QGVAR(in_air), false];

switch (true) do {
	case (_is_in_air): {
		_mr2text ctrlSetText (localize "STR_DOM_MISSIONSTRING_598");
		_mr2_available = false;
	};
	case (speed _mrs2 > 4): {
		_mr2text ctrlSetText (localize "STR_DOM_MISSIONSTRING_599");
		_mr2_available = false;
	};
	case (surfaceIsWater [(getPosASL _mrs2) select 0,(getPosASL _mrs2) select 1]): {
		_mr2text ctrlSetText (localize "STR_DOM_MISSIONSTRING_600");
		_mr2_available = false;
	};
	case (!alive _mrs2): {
		_mr2text ctrlSetText (localize "STR_DOM_MISSIONSTRING_601");
		_mr2_available = false;
	};
	default {
		_depl = GV(_mrs2,GVAR(MHQ_Deployed));
		if (isNil "_depl" || {!_depl}) then {
			_mr2text ctrlSetText (localize "STR_DOM_MISSIONSTRING_602");
			_mr2_available = false;
		} else {
			_enear = GV(_mrs2,GVAR(enemy_near));
			if (!isNil "_enear" && {_enear}) then {
				_mr2text ctrlSetText (localize "STR_DOM_MISSIONSTRING_603");
				_mr2_available = false;
			};
		};
	};
};

// TYRGHEN - Handling Leader availability
__TargetInfo
if (GVAR(with_dynamic_teleport) == 1) then {
	if (GVAR(player_leader_var) != "" && {!isnull (missionNameSpace getVariable GVAR(player_leader_var))}) then {
		_leader = (missionNameSpace getVariable GVAR(player_leader_var));
		_tl_available = (!isnull _leader && {_leader != player} && {alive _leader} && {vehicle _leader == _leader} && {!(_leader getVariable ["xr_pluncon",false])} && {(GVAR(teleport_in_town) == 0 || ((_target_array2 select 0) distance (vehicle _leader)) >= (_target_array2 select 2))});
	};
} else {
	_leader = leader (group player);
	//diag_log [!isnull _leader,_leader != player,alive _leader,!(GV(_leader,xr_pluncon))];
	_tl_available = (!isnull _leader && {_leader != player} && {alive _leader} && {vehicle _leader == _leader} && {!(_leader getVariable ["xr_pluncon",false])} && {(GVAR(teleport_in_town) == 0 || ((_target_array2 select 0) distance (vehicle _leader)) >= (_target_array2 select 2))});
};
if (isnil "_tl_available") then {_tl_available = false;};

// TYRGHEN - Handling bunkers availability
_camps = +(__XJIPGetVar(GVAR(currentcamps)));
if (!(isNil "_camps") && {count _camps > 0}) then {
	for "_i" from 0 to (count _camps - 1) do {
		if (((_camps select _i) getVariable [QGVAR(SIDE),GVAR(enemy_side)]) != GVAR(own_side_trigger_alt)) then {
			_camps set [_i,objNull];
		};
	};
	_camps = _camps - [objNull];
	_camps_available = (count _camps > 0);
};

if (GVAR(x_loop_end)) exitWith {};

if (!_mr1_available) then {
	__CTRL(100108) ctrlEnable false;
	if (GVAR(beam_target) == 1) then {
		GVAR(beam_target) = -1;
		__CTRL(100110) ctrlSetText "";
	};
} else {
	__CTRL(100108) ctrlEnable true;
};

if (!_mr2_available) then {
	__CTRL(100109) ctrlEnable false;
	if (GVAR(beam_target) == 2) then {
		GVAR(beam_target) = -1;
		__CTRL(100110) ctrlSetText "";
	};
} else {
	__CTRL(100109) ctrlEnable true;
};

// TYRGHEN - disabling the button when needed
if (!_tl_available) then {
	__CTRL(100113) ctrlEnable false;
	if (GVAR(beam_target) == 3) then {
		GVAR(beam_target) = -1;
		__CTRL(100110) ctrlSetText "";
	};
} else {
	__CTRL(100113) ctrlEnable true;
};

// TYRGHEN - disabling the button when needed
if (!_camps_available) then {
	__CTRL(100114) ctrlEnable false;
	if (GVAR(beam_target) == 4) then {
		GVAR(beam_target) = -1;
		__CTRL(100110) ctrlSetText "";
	};
} else {
	__CTRL(100114) ctrlEnable true;
};

